﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;

namespace TheCollectiveNetwork.Classes.Security
{
    public static class TCNauthentication
    {
        // Variables 
        static bool c_isAdminUser;
        static bool c_isEditAllUser;
        static bool c_isReadAllUser;
        static bool c_isNormalUser;
        static bool c_isAuthenticated;
        static bool c_RequiresPasswordChange;
        static String c_UserID;

        // Properties
        public static bool isAdminUser
        {
            get
            {
                return c_isAdminUser;
            }
        }

        public static bool isEditAllUser
        {
            get
            {
                return c_isEditAllUser;
            }
        }

        public static bool isReadAllUser
        {
            get
            {
                return c_isReadAllUser;
            }
        }

        public static bool isNormalUser
        {
            get
            {
                return c_isNormalUser;
            }
        }


        public static bool isAuthenticated
        {
            get
            {
                return c_isAuthenticated;
            }
        }

        public static bool RequiresPasswordChange
        {
            get
            {
                return c_RequiresPasswordChange;
            }
        }

        public static String UserID
        {
            get
            {
                return c_UserID;
            }
        }

        // Allow access is set if the user is authenticated and is in a least one valid role
        public static bool AllowAccess
        {
            get
            {
                return c_isAuthenticated && (c_isAdminUser || c_isEditAllUser || c_isReadAllUser || c_isNormalUser);
            }
        }


        // Public Functions

        /// <summary>
        /// Loads the profile of a user in to the security object
        /// </summary>
        public static void LoadProfile()
        {

            c_isAuthenticated = WebContext.Current.User.IsAuthenticated;
            c_RequiresPasswordChange = WebContext.Current.User.RequiresPasswordChange;
            c_UserID = WebContext.Current.User.Name;

            if (WebContext.Current.User.IsInRole("101"))
            {
                c_isAdminUser = true;

            }
            else
            {
                c_isAdminUser = false;
            }


            if (WebContext.Current.User.IsInRole("102"))
            {
                c_isEditAllUser = true;
            }
            else
            {
                c_isEditAllUser = false;
            }


            if (WebContext.Current.User.IsInRole("103"))
            {
                c_isReadAllUser = true;
            }
            else
            {
                c_isReadAllUser = false;
            }


            if (WebContext.Current.User.IsInRole("104"))
            {
                c_isNormalUser = true;
            }
            else
            {

                c_isNormalUser = false;
            }

                        
        }


          /// <summary>
        /// Logs the user in
        /// </summary>
        public static void Login(string username, string password, Action<LoginOperation> CompletedFunction)
        {
            LoginParameters loginparam;
            LoginOperation CurrentLoginOperation;

            // Do the login
            loginparam = new LoginParameters(username, password, false, null);
            CurrentLoginOperation =  WebContext.Current.Authentication.Login(loginparam, CompletedFunction, null);
            
            
        }

         

        /// <summary>
        /// Logs the user out
        /// </summary>
        public static void Logout(Action<LogoutOperation> CompletedFunction)
        {
            // Log the user out
            WebContext.Current.Authentication.Logout(CompletedFunction, null);

            // Set everthing to false
            c_isAuthenticated = false;          
            c_isAdminUser = false;    
            c_isEditAllUser = false;            
            c_isReadAllUser = false;         
            c_isNormalUser = false;
            c_RequiresPasswordChange = false;            
        }

                
    }
}
